﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameEngine.EntityComponents;
using GameEngine.Entities;
using GameEngine.Managers.IManagers;
using Microsoft.Xna.Framework.Audio;
using PvT.Entities;

namespace PvT.EntityComponent
{
    public class PlayerSoundComponent : IEntityComponent
    {
        private AbstractEntity entity;
        private IInputManager inputManager;
        private IAudioManager audioManager;
        private SoundEffect jumpSound;
        private SoundEffect collisionSoundPowerUp;
        private SoundEffect collisionSoundSkynet;
        private string jumpSName;
        private string collisionSNamePU;
        private string collisionSNameSN;


        /// <summary>
        /// Initializes a new instance of the <see cref="PlayerSoundComponent" /> class.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public PlayerSoundComponent(AbstractEntity entity)
        {
            this.entity = entity;
            inputManager = (IInputManager)entity.game.Services.GetService(typeof(IInputManager));
            audioManager = (IAudioManager)entity.game.Services.GetService(typeof(IAudioManager));
        }

        public void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (inputManager.IsAPressed())
            {
                MoveableComponent moveable = (MoveableComponent)entity.GetComponent(typeof(MoveableComponent));
                if (moveable != null && moveable.movementState == MoveableComponent.State.OnGround)
                {
                    // play jump sound
                    audioManager.PlaySound(jumpSName, 1.0f);
                }
            }
        }


        /// <summary>
        /// Gets or sets the jump sound.
        /// </summary>
        /// <value>
        /// The jump sound.
        /// </value>
        public string JumpSound
        {
            get
            {
                return jumpSName;
            }
            set
            {
                jumpSName = value;
                jumpSound = entity.game.Content.Load<SoundEffect>(jumpSName);
                audioManager.AddSoundEffect(jumpSound, jumpSName);
            }
        }



        /// <summary>
        /// Gets or sets the collision sound power up.
        /// </summary>
        /// <value>
        /// The collision sound power up.
        /// </value>
        public string CollisionSoundPowerUp
        {
            get
            {
                return collisionSNamePU;
            }
            set
            {
                collisionSNamePU = value;
                collisionSoundPowerUp = entity.game.Content.Load<SoundEffect>(collisionSNamePU);
                audioManager.AddSoundEffect(collisionSoundPowerUp, collisionSNamePU);
            }
        }


        /// <summary>
        /// Gets or sets the collision sound skynet.
        /// </summary>
        /// <value>
        /// The collision sound skynet.
        /// </value>
        public string CollisionSoundSkynet
        {
            get
            {
                return collisionSNameSN;
            }
            set
            {
                collisionSNameSN = value;
                collisionSoundSkynet = entity.game.Content.Load<SoundEffect>(collisionSNameSN);
                audioManager.AddSoundEffect(collisionSoundSkynet, collisionSNameSN);
            }
        }




        /// <summary>
        /// This method decides what to do if a collision occurs with this component
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="isX">if set to <c>true</c> [is X].</param>
        /// <param name="isY">if set to <c>true</c> [is Y].</param>
        public void CollideWith(AbstractEntity target, bool isX, bool isY)
        {
            // play collide sound
            if (target is PowerUpEntity)
            {
                audioManager.PlaySound(collisionSNamePU, 1.0f);
            }

            //if (target is Skynet)
            //{
            //    audioManager.PlaySound(collisionSNameSN, 1.0f);
            //}
        }
    }
}
